package com.zjut.an.news.action;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zjut.an.news.dao.AnNews;
import com.zjut.an.news.manager.AnNewsManager;
import com.zjut.an.util.module.PageModule;
import com.zjut.an.util.system.Status;
import com.zjut.an.util.tools.JSONUtilClass;
import com.zjut.an.util.tools.JSONUtilClassTime;
import com.zjut.an.webclass.manager.AnWebclassManager;

public class AnNewsAction extends ActionSupport implements
ServletResponseAware, ServletRequestAware, ModelDriven<AnNews>{

	private HttpServletRequest request;
	private HttpServletResponse response;
	private HttpSession session;

	private AnNews obj;
	private Map<String , Object> dataMap;
	private int page = 1, limit = 10;
	private String order;
	private String sort;
	private String search;
	private String rowstr;

	@Resource(name = "AnNewsManager")
	private AnNewsManager manager;

	@Resource(name = "AnWebclassManager")
	private AnWebclassManager wmanager;

	public String anNewsList() {
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e) {
			return ERROR; // 或根据业务逻辑处理异常
		}

		dataMap = new HashMap<String, Object>();
		if (manager != null && obj != null) {
			String type = request.getParameter("type");
			if ("public".equals(type)) {
				obj.setIscheck(Status.ISCHECK_YES.getStatus());
			} else if ("all".equals(type)) {
				obj.setIscheck(null); // 清除筛选条件
			}
			PageModule module = manager.getDataByInstance(page, limit, sort, order, obj);
			if (module != null) {
				dataMap.put("rows", module.getObjects());
				dataMap.put("total", module.getTotal());
			}
		}
		obj = null; // 明确释放对象引用
		return SUCCESS;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String anNewsListCheck() throws UnsupportedEncodingException {
		if (session.getAttribute("admin")!=null) {//只有管理员才允许执行该操作，以免信息泄露
			request.setCharacterEncoding("UTF-8");
			dataMap = new HashMap<String, Object>();

			if(search!=null) {
				obj = new AnNews();
				obj.setTitle(search);
				search = null;
			}
			obj.setIscheck(Status.ISCHECK_NO.getStatus());
			PageModule module = manager.getDataByInstance(page, limit, sort, order, obj);
			dataMap.put("rows", module.getObjects());
			dataMap.put("total", module.getTotal());
			obj = null;
		}
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String anNewsDeletes() {
		if (session.getAttribute("admin")!=null) {//只有管理员才允许执行该操作，以免信息泄露
			String objumniJSON = rowstr;
			dataMap = new HashMap<String, Object>();
			try {
				manager.deleteDatas(JSONUtilClass.getDTOList(objumniJSON, AnNews.class));
				dataMap.put("flag", "success");
			} catch (Exception e) {
				// TODO: handle exception
				dataMap.put("flag", "error");
			}
		}
		return SUCCESS;
	}

	public String anNewsAdd() throws UnsupportedEncodingException {
		if (session.getAttribute("admin")!=null) {//只有管理员才允许执行该操作，以免信息泄露
			request.setCharacterEncoding("UTF-8");
			dataMap = new HashMap<String, Object>();
			String objumniJSON = rowstr;
			System.out.println("1111"+objumniJSON);
			//System.out.println(JSONUtilClass.getMapFromJson(objumniJSON).get("picture"));
			AnNews news = (AnNews)JSONUtilClass.getDTO(objumniJSON, AnNews.class);
			//news.setPicture(news.getPicture().replace(",", ""));//替换前台采集带,符号
			//news.setIscheck(Status.ISCHECK_YES.getStatus());
			//System.out.println("2222"+objumniJSON);
			manager.addData(news);
			//System.out.println("33333"+objumniJSON);
			obj = null;
			news = null;
			dataMap.put("flag", "success");
		}
		return SUCCESS;
	}

	public String anNewsDelete() {
		if (session.getAttribute("admin")!=null) {//只有管理员才允许执行该操作，以免信息泄露
			String objumniJSON = rowstr;
			dataMap = new HashMap<String, Object>();
			try {
				manager.deleteData((AnNews)JSONUtilClass.getDTO(objumniJSON, AnNews.class));
				dataMap.put("flag", "success");
			} catch (Exception e) {
				dataMap.put("flag", "error");
			}
		}
		return SUCCESS;
	}

	public String anNewsUpdate() throws UnsupportedEncodingException {
		if (session.getAttribute("admin")!=null) {//只有管理员才允许执行该操作，以免信息泄露
			request.setCharacterEncoding("UTF-8");
			dataMap = new HashMap<String, Object>();
			String objumniJSON = rowstr;
			AnNews news = (AnNews)JSONUtilClassTime.getDTO(objumniJSON, AnNews.class);
			manager.updateData(news);

			obj = null;
			news = null;
			dataMap.put("flag", "success");
		}
		return SUCCESS;
	}

	public String anNewsSave() {
		if (session.getAttribute("admin")!=null) {//只有管理员才允许执行该操作，以免信息泄露
			obj = (AnNews)(JSONUtilClass.getDTOArray(rowstr, AnNews.class)[0]);
			if (obj.getAnWebclass()!=null) {
				//-------update-------
				manager.updateData(obj);
			} else {
				//-------insert-------
				manager.addData(obj);
			}
			rowstr = null;
			obj = null;
		}

		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String anNewsChecks() throws UnsupportedEncodingException {
		if (session.getAttribute("admin")!=null) {//只有管理员才允许执行该操作，以免信息泄露
			request.setCharacterEncoding("UTF-8");
			String objumniJSON = rowstr;
			dataMap = new HashMap<String, Object>();
			try {
				List<AnNews> objs = JSONUtilClassTime.getDTOList(objumniJSON, AnNews.class);
				for (AnNews anNews : objs) {
					anNews.setIscheck(request.getParameter("ischeck"));
					manager.updateData(anNews);
				}
				dataMap.put("flag", "success");
			} catch (Exception e) {
				// TODO: handle exception
				dataMap.put("flag", "error");
			}
			obj = null;
		}
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String anNewsRecommends() throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		if (session.getAttribute("admin")!=null) {//只有管理员才允许执行该操作，以免信息泄露
			String objumniJSON = rowstr;
			dataMap = new HashMap<String, Object>();
			try {
				List<AnNews> objs = JSONUtilClassTime.getDTOList(objumniJSON, AnNews.class);
				for (AnNews anNews : objs) {
					anNews.setIsrecommend(request.getParameter("isrecommend"));
					manager.updateData(anNews);
				}
				dataMap.put("flag", "success");
			} catch (Exception e) {
				// TODO: handle exception
				dataMap.put("flag", "error");
			}
			obj = null;
		}

		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String anNewsTops() throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		if (session.getAttribute("admin")!=null) {//只有管理员才允许执行该操作，以免信息泄露
			String objumniJSON = rowstr;
			List<AnNews> objs = JSONUtilClassTime.getDTOList(objumniJSON, AnNews.class);
			for (AnNews anNews : objs) {
				anNews.setIstop(request.getParameter("istop"));
				manager.updateData(anNews);
			}
			obj = null;
		}
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String anNewsHots() throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		if (session.getAttribute("admin")!=null) {//只有管理员才允许执行该操作，以免信息泄露
			String objumniJSON = rowstr;
			List<AnNews> objs = JSONUtilClassTime.getDTOList(objumniJSON, AnNews.class);
			for (AnNews anNews : objs) {
				anNews.setIshot(request.getParameter("ishot"));
				manager.updateData(anNews);
			}
			obj = null;
		}
		return SUCCESS;
	}

	public String anNews() throws UnsupportedEncodingException {
		request.setCharacterEncoding("UTF-8");
		dataMap = new HashMap<String, Object>();
		Integer neId = Integer.parseInt(request.getParameter("neId"));
		obj = manager.getData(neId);
		dataMap.put("rows", obj);
		obj = null;
		return SUCCESS;
	}

	@Override
	public AnNews getModel() {
		if (null == obj) {
			obj = new AnNews();
		}
		return obj;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
		session = request.getSession();
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		response = arg0;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public AnNews getAl() {
		return obj;
	}

	public void setAl(AnNews obj) {
		this.obj = obj;
	}

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}

	public String getRowstr() {
		return rowstr;
	}

	public void setRowstr(String rowstr) {
		this.rowstr = rowstr;
	}

	public AnNewsManager getManager() {
		return manager;
	}

	public void setManager(AnNewsManager manager) {
		this.manager = manager;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

}
